import { getprofile, loginAPI } from "@/api/user"
import { getToken, removeToken, setToken } from "@/utils/auth"
import Cookies from 'js-cookie'
import { TOKEN_KEY } from '@/constants/KEY'
export default {
  namespaced: true,
  state: () => {
    return {
      token: getToken() || "",
      profile: {}
    }

  },
  mutations: {
    setProfile(state, profile) {
      state.profile = profile

    },
    clearuserinfo(state) {
      state.token = "";
      removeToken();

    },
    setToken(state, token) {
      state.token = token
      setToken(token)
      console.log(Cookies.get(TOKEN_KEY),"Cookies.get(TOKEN_KEY)");
    }
  },
  actions: {
    async doLogin(ctx, { username, password }) {
      console.log(username, "username");
      const res = await loginAPI({ username, password });
      ctx.commit('setToken', res.data.token)
      const res1 = await getprofile();
      console.log(res1,"reff");
      ctx.commit("setProfile", res1.data)
      //  ctx.commit('setProfile', getprofilelist(ctx))
      return res1.data.permissions
    },
    async getprofilelist(ctx) {
      const res = await getprofile();
      console.log(res,"reff");
      ctx.commit("setProfile", res.data)
      return res.data.permissions
    }
  }
}
